import random
import time
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ee
from selenium.webdriver.support.wait import WebDriverWait
from multiprocessing import Pool
from tianyancha import const
from tianyancha.mongo import mongoDb


# http://www.tianyancha.com/search/oc1200/p1
class tySpider:
    cusHeaders = DesiredCapabilities.PHANTOMJS.copy()
    cusHeaders['phantomjs.page.customHeaders.User-Agent'] = const.user_agent

    def __init__(self, waitTime=30, searchTime=0.3):
        self.webdriver = webdriver.PhantomJS(desired_capabilities=self.cusHeaders)
        self.mainGysInfo = []
        self.industryPages = self.getIndusturyPages()
        self.waitTime = waitTime
        self.searchTime = searchTime

    def getIndusturyPages(self):
        pages = []
        with mongoDb('industry') as industry:
            for ind in industry.findOutList("secList", {}):
                for i in range(1, 51):
                    pages.append("http://www.tianyancha.com/search/oc%s/p%s" % (ind["secInduCode"], i))
        return pages

    def getMainByPage(self, page):
        print('entering the page %s' % page)
        self.webdriver.get(page)
        locate = (By.CSS_SELECTOR, "div.search_right_item")
        try:
            WebDriverWait(self.webdriver, self.waitTime, self.searchTime).until(ee.presence_of_element_located(locate))
            for item in self.webdriver.find_elements_by_css_selector('div.search_right_item'):
                href = item.find_element_by_css_selector('div.search_repadding2 > a').get_attribute('href')
                companyId = href.rsplit('/')[-1]
                companyName = item.find_element_by_css_selector('a.query_name span').text
                companyAgent = \
                    item.find_element_by_css_selector('div.search_row_new div:nth-child(1)').text.split("： ")[-1]
                companyMoney = \
                    item.find_element_by_css_selector('div.search_row_new div:nth-child(2)').text.split("： ")[-1]
                companyRegDate = \
                    item.find_element_by_css_selector('div.search_row_new div:nth-child(3)').text.split("：")[-1]
                companyBase = item.find_element_by_css_selector('div.search_base.position-rel').text.split('\n')
                companyPosition = companyBase[0]
                companyScore = companyBase[1]
                companyIsLive = companyBase[3]
                companyInfo = {
                    "companyId": companyId,
                    "companyName": companyName,
                    "href": href,
                    "companyAgent": companyAgent,
                    "companyMoney": companyMoney,
                    "companyRegDate": companyRegDate,
                    "companyPosition": companyPosition,
                    "companyScore": companyScore,
                    "companyIsLive": companyIsLive
                }
                self.saveOne("gys_mainInfo", companyInfo)
        except ee:
            print('find this page :%s is failuer!!' % page)
            print(ee)
            pass

    def getGysContent(self, page):
        self.webdriver.get(page)
        locator = (By.CSS_SELECTOR, 'div.company_info_text>div.ng-binding')
        try:
            WebDriverWait(self.webdriver, self.waitTime, self.searchTime).until(ee.presence_of_element_located(locator))
            companyId = page.rsplit('/')[-1]
            companyName = self.webdriver.find_element_by_css_selector('div.company_info_text>div.mb5').text
            telPhone = \
                self.webdriver.find_element_by_css_selector('div.company_info_text > span:nth-child(4)').text.split(
                    ": ")[
                    -1]
            email = \
                self.webdriver.find_element_by_css_selector('div.company_info_text > span:nth-child(5)').text.split(
                    ": ")[
                    -1]
            address = \
                self.webdriver.find_element_by_css_selector('div.company_info_text > span:nth-child(9)').text.split(
                    ": ")[
                    -1]
            website = \
                self.webdriver.find_element_by_css_selector('span[ng-hide="company.websiteList"]').text.split(": ")[-1]

            containTable = self.webdriver.find_element_by_css_selector('div.company-content table')
            # 工商注册号
            regNumber = containTable.find_element_by_css_selector('tr:nth-child(1) td:nth-child(1)').text.split(': ')[
                -1]
            # 组织机构代码
            orgNumber = containTable.find_element_by_css_selector('tr:nth-child(1) td:nth-child(2)').text.split('：')[
                -1]
            # 统一信用代码
            creditCode = containTable.find_element_by_css_selector('tr:nth-child(2) td:nth-child(1)').text.split('：')[
                -1]
            # companyOrgType=有限责任公司（非自然人投资或控股的法人独资）
            companyOrgType = \
                containTable.find_element_by_css_selector('tr:nth-child(2) td:nth-child(2)').text.split('：')[-1]
            # industry=批发业
            industry = containTable.find_element_by_css_selector('tr:nth-child(3) td:nth-child(1)').text.split('：')[-1]
            # fromTime=1293638400000
            fromTime = containTable.find_element_by_css_selector('tr:nth-child(3) td:nth-child(2)').text.split('：')[-1]
            # businessScope=矿产品、铁精粉
            businessScope = \
                containTable.find_element_by_css_selector('tr:nth-child(6) td:nth-child(1)').text.split('：')[-1]
            lawsuitCount = 0
            try:
                lawsuitCount = self.webdriver.find_element_by_css_selector('div#nav-main-lawsuitCount span.c9').text
            except:
                pass
            companyContent = {
                "companyId": companyId,
                "companyName": companyName,
                "telPhone": telPhone,
                "email": email,
                "address": address,
                "website": website,
                "regNumber": regNumber,
                "orgNumber": orgNumber,
                "companyOrgType": companyOrgType,
                "industry": industry,
                "fromTime": fromTime,
                "businessScope": businessScope,
                "lawsuitCount": lawsuitCount
            }
            self.saveOne("gys_content", companyContent)
        except ee:
            print('get getGysContent(self,%s) is fail' % page)
            pass

    def saveMany(self, doc, list):
        try:
            with mongoDb(doc) as main:
                main.insert(list)
            print('insert data is ok!!!!!!!!!!')
        except:
            print('insert data is fail!!!!!!!!!!')
            pass

    def saveOne(self, doc, item):
        try:
            with mongoDb(doc) as main:
                main.insert_one(item)
            print('insert data is ok!!!!!!!!!!')
        except:
            print('insert data is fail!!!!!!!!!')
            pass

    def __enter__(self):
        self.getIndusturyPages()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.webdriver.quit()


if __name__ == '__main__':
    t = tySpider()
    for page in t.industryPages:
        t.getMainByPage(page)
        time.sleep(random.randrange(1,10))
